package yxy.game.pm2.active.command.belovedhero;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actfavoritewarlordreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心仪武将活动;
import yxy.game.pm2.active.module.actfavoritewarlord.FavouriteTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;

/****************************************
 * @name CMD_Active_心仪武将_ActBelovedHeroLoginDrawReward
 * @description 心仪武将登录福利领取
 * @author 梁健彬
 * @since 2022-07-8
 ***************************************/
@MessageType(proto = Command.ActBelovedHeroLoginDrawReward, classtype = ActiveMessage.ActBelovedHeroLoginDrawRewardRequest.class)
public class CMD_Active_心仪武将_ActBelovedHeroLoginDrawReward extends Command<ActiveMessage.ActBelovedHeroLoginDrawRewardRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActBelovedHeroLoginDrawRewardRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActBelovedHeroLoginDrawRewardResponse.Builder response = ActiveMessage.ActBelovedHeroLoginDrawRewardResponse.newBuilder();
        int id = request.getId();//任务的id
        try {
            ActiveModule_心仪武将活动 am = ActiveModule_心仪武将活动.get(playercode, false);

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.getTaskMap().containsKey(id)) {
                send(messageSource, response, Result.DataError);//如果不包含该id，参数错误
                return;
            }

            ActBelovedHeroHandle heroHandle = new ActBelovedHeroHandle();
            //todo 根据id领奖，如果能领就领取（免费的和付费的任务）
            FavouriteTask task = am.getTaskMap().get(id);
            Task freeTaskBean = Task.get(playercode, task.getFreeTaskCode());//免费的任务
            Task preTaskBean = Task.get(playercode, task.getPreTaskCode());//付费的任务

            if (freeTaskBean == null || preTaskBean == null) {
                send(messageSource, response, Result.DataError);//task不存在
                return;
            }

            if (freeTaskBean.isReward() && preTaskBean.isReward()) {
                send(messageSource, response, Result.ActHadGet);//两个都已经领取过了
                return;
            }

            if (!freeTaskBean.isFinish() && !preTaskBean.isFinish()) {
                send(messageSource, response, Result.ActCannotGet);//都没有完成则不能领取
                return;
            }

            actfavoritewarlordreward data = DataCenter.getData(id, actfavoritewarlordreward.class);//奖励表
            if (data == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            //获取已完成的奖励
            ArrayList<Currency> currencies = heroHandle.getCurs(am, freeTaskBean, preTaskBean, task, playercode, data);
            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(currencies);

            response.addAllItem(items);//返回奖励信息

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, ActiveMessage.ActBelovedHeroLoginDrawRewardResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
